import time
import rawsc
import rawsc.service
from rawsc import rass
import rawsc.rass.service
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

USER = "XXX"
PWD = "XXX"
SERVER = "rass.cdn01.rambla.be"

try:
    # Create a Rass instance, passing it your login credentials and the base service uri
    client = rawsc.rass.service.RassService(username=USER, password=PWD, server=SERVER)

    # get list of dirs
    qry = rawsc.Query()
    qry["kind"] = "dir"
    qry["paginate_by"] = "20" # each response may contain 20 dir entries
    feed = client.getDirFeed(uri = "/", query = qry)
    print "Retrieved feed of directories (kind=dir):"
    
    # get the first 20 entries
    for e in feed.entry:
        print "\nDir Entry URI: " + str(e.id.text)
        print "name: " + str(e.content.params.name.text)
        print "size: " + str(e.content.params.size.text)
        print "updated: " + str(e.content.params.updated.text)
        print "enclosure: " + str(rawsc.get_enclosure_link(e))
    next_page = rawsc.get_next_link(feed)
    
    # as long as the feed contains a 'next' link, keep retrieving the next 20 entries
    while next_page:
        feed = client.getDirFeed(uri = next_page)
        print "Retrieved feed of directories (kind=dir):"
        for e in feed.entry:
            print "\nDir Entry URI: " + str(e.id.text)
            print "name: " + str(e.content.params.name.text)
            print "size: " + str(e.content.params.size.text)
            print "updated: " + str(e.content.params.updated.text)
            print "enclosure: " + str(rawsc.get_enclosure_link(e))
        next_page = rawsc.get_next_link(feed)

except rawsc.service.Error, e:
    print "Error Response from server, contents = %s.\n" % str(e)
    error_dict = e[0] # error dict inside the first tuple elem
    print "status code : " + str(error_dict["status"]) + "\n"
    if error_dict.has_key("body"):
        print "error body : " + error_dict["body"] + "\n"
except rawsc.RawscException, e:
    print "RawscException caught, reason = %s.\n" % str(e)
except StandardError, e:
    print "StandardError caught, reason = %s.\n" % str(e)
except:
    print "unhandled exception caught\n"
